﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using System.Collections;
namespace TS_XY_Train_Repaire.Locomotive.MainBase.Fittings
{
    public partial class Locomotive_MainBase_Fittings_Simple_Info_Bodie_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID = "1";
        public Locomotive_MainBase_Fittings_Simple_Info_Bodie_ManaForm()
        {
            InitializeComponent();
        }

        private void Locomotive_MainBase_Fittings_Simple_Info_Bodie_ManaForm_Load(object sender, EventArgs e)
        {
            InitData();
            TS_Win_Class.FillListTypeAct(cb_State, "select f_id,f_name from TS_TR_B_Loco_Fittings_State where f_del=0");
        }

        private void Locomotive_MainBase_Fittings_Simple_Info_Bodie_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }
        private void InitData()
        {
            tl_SysDep.Nodes.Clear();
            ArrayList pList = new ArrayList();
            LeverString_TrainDepSysList p = new LeverString_TrainDepSysList();
            string strSQL = "select f_id,f_name from TS_S_T_P_TrainClass where f_del=0 order by f_id asc"; ;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new LeverString_TrainDepSysList();
                p.f_id = "C" + read["f_id"].ToString();
                p.f_name = read["f_name"].ToString();
                p.f_imgindex = 0;
                pList.Add(p);
            }
            read.Close();
            strSQL = "select * from TS_S_T_P_TrainDepSys where f_deep=0 and f_del=0";
            comm = new SqlCommand(strSQL, conn);
            read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new LeverString_TrainDepSysList();
                p.f_name = read["f_name"].ToString();
                p.f_id = "S" + read["f_id"].ToString();
                p.f_parent = "C" + read["f_tcid"].ToString();
                p.f_deep = read["f_deep"].ToString();
                p.f_level = read["f_level"].ToString();
                p.f_tdid = read["f_id"].ToString();
                p.f_imgindex = 1;
                pList.Add(p);
            }
            read.Close();
            strSQL = "select f_id,f_name as f_name,f_tcid,f_deep,f_level from TS_S_T_P_TrainDepSys where f_deep>0 and f_type in (9," + _LoadID + ") and f_del=0 order by f_name asc";
            comm = new SqlCommand(strSQL, conn);
            read = comm.ExecuteReader();
            while (read.Read())
            {
                string _PID = read["f_level"].ToString();
                _PID = _PID.Substring(0, _PID.Length - 1);
                _PID = _PID.Substring(_PID.LastIndexOf(',') + 1);
                p = new LeverString_TrainDepSysList();
                p.f_name = read["f_name"].ToString();
                p.f_id = "S" + read["f_id"].ToString();
                p.f_parent = "S" + _PID;
                p.f_deep = read["f_deep"].ToString();
                p.f_level = read["f_level"].ToString();
                p.f_tcid = read["f_tcid"].ToString();
                p.f_tdid = read["f_id"].ToString();
                p.f_imgindex = 1;
                pList.Add(p);
            }
            read.Close();
            strSQL = "select f_id,f_name,f_tdid from TS_S_T_P_TrainDepSys_Type where f_del=0 and f_tdid in (select f_id from TS_S_T_P_TrainDepSys where f_deep>0 and f_type in (9," + _LoadID + ") and f_del=0)";
            comm = new SqlCommand(strSQL, conn);
            read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new LeverString_TrainDepSysList();
                p.f_name = read["f_name"].ToString();
                p.f_id = "T" + read["f_id"].ToString();
                p.f_tdid = read["f_tdid"].ToString();
                p.f_imgindex = 2;
                p.f_parent = "S" + read["f_tdid"].ToString();
                pList.Add(p);
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            tl_SysDep.DataSource = pList;
        }

        private void tl_SysDep_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            try
            {
                if (tl_SysDep.FocusedNode["f_id"].ToString().Substring(0, 1) == "T")
                {
                    bt_Add.Enabled = true;
                    bt_Edit.Enabled = false;
                    bt_Del.Enabled = false;
                    gb_Type.Text = tl_SysDep.FocusedNode["f_name"].ToString() + "型号配件信息录入";
                }
                else
                {
                    bt_Add.Enabled = false;
                    bt_Edit.Enabled = false;
                    bt_Del.Enabled = false;
                    gc_Fittings.DataSource = null;
                    gc_Fittings.DefaultView.RefreshData();
                    gb_Type.Text = "配件信息录入";
                }
                f_tdname.Visible = false;
                f_ftname.Visible = false;
                f_fname.Visible = false;
                f_sname.Visible = false;
                f_count.Visible = false;
                f_place.Visible = false;
                f_lownum.Visible = false;
                f_upnum.Visible = false;
                switch (tl_SysDep.FocusedNode["f_id"].ToString().Substring(0, 1) )
                {
                    case "T":
                        f_ftname.Visible = true;
                        f_fname.Visible = true;
                        f_sname.Visible = true;
                        f_place.Visible = true;
                        f_ftname.VisibleIndex = 0;
                        f_fname.VisibleIndex = 1;
                        f_sname.VisibleIndex = 2;
                        f_place.VisibleIndex = 3;
                        InitFittingsGrid("select f_id,f_code as f_fname,f_state,f_mapid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_TR_B_Loco_Fittings_Base_List.f_mapid) as f_place,(select f_name from TS_S_T_P_TrainDepSys where f_id=TS_TR_B_Loco_Fittings_Base_List.f_tdid) as f_tdname,(select f_name from TS_TR_B_Loco_Fittings_State where f_id=TS_TR_B_Loco_Fittings_Base_List.f_state) as f_sname,(select f_name from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_B_Loco_Fittings_Base_List.f_typeid) as f_ftname from TS_TR_B_Loco_Fittings_Base_List where f_typeid=" + tl_SysDep.FocusedNode["f_id"].ToString().Substring(1) + " and f_del=0 ");
                        break;
                    case "S":
                        f_tdname.Visible = true;
                        f_ftname.Visible = true;
                        f_sname.Visible = true;
                        f_count.Visible = true;
                        f_lownum.Visible = true;
                        f_upnum.Visible = true;
                        f_tdname.VisibleIndex = 0;
                        f_ftname.VisibleIndex = 1;
                        f_sname.VisibleIndex = 2;
                        f_count.VisibleIndex = 3;
                        f_lownum.VisibleIndex = 4;
                        f_upnum.VisibleIndex = 5;
                        InitFittingsGrid("select (select f_name from TS_S_T_P_TrainDepSys where f_id=TS_TR_B_Loco_Fittings_Base_List.f_tdid) as f_tdname,(select f_name from TS_TR_B_Loco_Fittings_State where f_id=TS_TR_B_Loco_Fittings_Base_List.f_state) as f_sname,count(*) as f_count,(select f_lownum from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_B_Loco_Fittings_Base_List.f_typeid) as f_lownum,(select f_upnum from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_B_Loco_Fittings_Base_List.f_typeid) as f_upnum,(select f_name from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_B_Loco_Fittings_Base_List.f_typeid) as f_ftname from TS_TR_B_Loco_Fittings_Base_List where f_tdid in(select f_id from TS_S_T_P_TrainDepSys where (f_level like '%" + tl_SysDep.FocusedNode["f_level"].ToString() + tl_SysDep.FocusedNode["f_id"].ToString().Substring(1) + ",%' or f_id=" + tl_SysDep.FocusedNode["f_id"].ToString().Substring(1) + ") and f_del=0) and f_del=0 group by f_tdid,f_typeid,f_state");
                        break;
                    case "C":
                        f_tdname.Visible = true;
                        f_ftname.Visible = true;
                        f_sname.Visible = true;
                        f_count.Visible = true;
                        f_lownum.Visible = true;
                        f_upnum.Visible = true;
                        f_tdname.VisibleIndex = 0;
                        f_ftname.VisibleIndex = 1;
                        f_sname.VisibleIndex = 2;
                        f_count.VisibleIndex = 3;
                        f_lownum.VisibleIndex = 4;
                        f_upnum.VisibleIndex = 5;
                        InitFittingsGrid("select (select f_name from TS_S_T_P_TrainDepSys where f_id=TS_TR_B_Loco_Fittings_Base_List.f_tdid) as f_tdname,(select f_name from TS_TR_B_Loco_Fittings_State where f_id=TS_TR_B_Loco_Fittings_Base_List.f_state) as f_sname,count(*) as f_count,(select f_lownum from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_B_Loco_Fittings_Base_List.f_typeid) as f_lownum,(select f_upnum from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_B_Loco_Fittings_Base_List.f_typeid) as f_upnum,(select f_name from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_B_Loco_Fittings_Base_List.f_typeid) as f_ftname from TS_TR_B_Loco_Fittings_Base_List where f_typeid in(select f_id from TS_S_T_P_TrainDepSys_Type where f_tcid=" + tl_SysDep.FocusedNode["f_id"].ToString().Substring(1) + " and f_del=0) and f_del=0 group by f_tdid,f_typeid,f_state");
                        break;
                }
            }
            catch
            {
                bt_Add.Enabled = false;
                bt_Edit.Enabled = false;
                bt_Del.Enabled = false;
                gc_Fittings.DataSource = null;
                gc_Fittings.DefaultView.RefreshData();
                gb_Type.Text = "配件信息录入";
            }
        }
        private void InitFittingsGrid(string str)
        {
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable(str, dt);
            gc_Fittings.DataSource = dt;
        }

        private void gc_Fittings_Click(object sender, EventArgs e)
        {
            try
            {
                if (gv_Fittings.SelectedRowsCount > 0)
                {
                    if (tl_SysDep.FocusedNode["f_id"].ToString().Substring(0, 1) == "T")
                    {
                        bt_Edit.Enabled = true;
                        bt_Del.Enabled = true;
                        te_Code.Text = gv_Fittings.GetRowCellValue(gv_Fittings.GetSelectedRows()[0], "f_fname").ToString();
                        cb_State.SelectedValue = gv_Fittings.GetRowCellValue(gv_Fittings.GetSelectedRows()[0], "f_state").ToString();
                        te_place.Text = gv_Fittings.GetRowCellValue(gv_Fittings.GetSelectedRows()[0], "f_place").ToString();
                        te_place.Tag = gv_Fittings.GetRowCellValue(gv_Fittings.GetSelectedRows()[0], "f_mapid").ToString();
                    }
                    else
                    {
                        bt_Edit.Enabled = false;
                        bt_Del.Enabled = false;
                    }
                }
                else
                {
                    bt_Edit.Enabled = false;
                    bt_Del.Enabled = false;
                }
            }
            catch
            {
                bt_Edit.Enabled = false;
                bt_Del.Enabled = false;
            }
        }

        private void cb_Batch_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_Batch.Checked)
            {
                te_Batch.Enabled = true;
            }
            else
            {
                te_Batch.Enabled = false;
            }
        }

        private void bt_Add_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            if (te_Code.Text.Trim().Length > 0 && cb_State.SelectedIndex>-1)
            {
                strSQL = "insert into TS_TR_B_Loco_Fittings_Base_List(f_typeid,f_tdid,f_name,f_code,f_workerid,f_mapid,f_state) values(" + tl_SysDep.FocusedNode["f_id"].ToString().Substring(1) + "," + tl_SysDep.FocusedNode["f_tdid"].ToString() + ",'" + tl_SysDep.FocusedNode["f_name"].ToString() + "','" + te_Code.Text.Trim() + "'," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ","+te_place.Tag.ToString()+","+cb_State.SelectedValue.ToString()+")";
                
                string _ID=TS_Win_Class.MySqlComGetID(strSQL);
                //strSQL = "update TS_FI_B_Loco_Fittings_Base_List set f_indate=dateadd(day,(select f_indate from TS_S_T_P_TrainDepSys where f_id=TS_FI_B_Loco_Fittings_Base_List.f_tdid),f_fcdate) where f_id=" + _ID;
                //TS_Win_Class.MySqlCom(strSQL);
                InitFittingsGrid("select f_id,f_code as f_fname,f_state,f_mapid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_TR_B_Loco_Fittings_Base_List.f_mapid) as f_place,(select f_name from TS_S_T_P_TrainDepSys where f_id=TS_TR_B_Loco_Fittings_Base_List.f_tdid) as f_tdname,(select f_name from TS_TR_B_Loco_Fittings_State where f_id=TS_TR_B_Loco_Fittings_Base_List.f_state) as f_sname,(select f_name from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_B_Loco_Fittings_Base_List.f_typeid) as f_ftname from TS_TR_B_Loco_Fittings_Base_List where f_typeid=" + tl_SysDep.FocusedNode["f_id"].ToString().Substring(1) + " and f_del=0 ");
                bt_Edit.Enabled = false;
                bt_Del.Enabled = false;
            }
            else
            {
                MessageBox.Show("请正确填写各项内容，并选定存放位置！");
            }
        }

        private void te_place_DoubleClick(object sender, EventArgs e)
        {
            //uc_Pub_Map_Select_ListView_ByOne_CallBack uc = new uc_Pub_Map_Select_ListView_ByOne_CallBack();
            //uc._LoadType = "1";
            //uc.Tag = te_place;
            //if (uc.ShowDialog() == DialogResult.OK)
            //{
            //}
            //else
            //{
            //}
        }

        private void bt_Edit_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            if (te_Code.Text.Trim().Length > 0 && te_place.Tag.ToString() != "-1" && cb_State.SelectedIndex > -1)
            {
                strSQL = "update TS_TR_B_Loco_Fittings_Base_List set f_state=" + cb_State.SelectedValue.ToString() + ",f_code='" + te_Code.Text.Trim() + "',f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_mapid=" + te_place.Tag.ToString() + ",f_time=getdate() where f_id=" + gv_Fittings.GetRowCellValue(gv_Fittings.GetSelectedRows()[0], "f_id").ToString();
                TS_Win_Class.MySqlCom(strSQL);
                InitFittingsGrid("select f_id,f_code as f_fname,f_state,f_mapid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_TR_B_Loco_Fittings_Base_List.f_mapid) as f_place,(select f_name from TS_S_T_P_TrainDepSys where f_id=TS_TR_B_Loco_Fittings_Base_List.f_tdid) as f_tdname,(select f_name from TS_TR_B_Loco_Fittings_State where f_id=TS_TR_B_Loco_Fittings_Base_List.f_state) as f_sname,(select f_name from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_B_Loco_Fittings_Base_List.f_typeid) as f_ftname from TS_TR_B_Loco_Fittings_Base_List where f_typeid=" + tl_SysDep.FocusedNode["f_id"].ToString().Substring(1) + " and f_del=0 ");
                bt_Edit.Enabled = false;
                bt_Del.Enabled = false;
            }
            else
            {
                MessageBox.Show("请正确填写各项内容，并选定存放位置！");
            }
        }

        private void bt_Del_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            strSQL = "update TS_TR_B_Loco_Fittings_Base_List set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + gv_Fittings.GetRowCellValue(gv_Fittings.GetSelectedRows()[0], "f_id").ToString();
            TS_Win_Class.MySqlCom(strSQL);
            InitFittingsGrid("select f_id,f_code as f_fname,f_state,f_mapid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_TR_B_Loco_Fittings_Base_List.f_mapid) as f_place,(select f_name from TS_S_T_P_TrainDepSys where f_id=TS_TR_B_Loco_Fittings_Base_List.f_tdid) as f_tdname,(select f_name from TS_TR_B_Loco_Fittings_State where f_id=TS_TR_B_Loco_Fittings_Base_List.f_state) as f_sname,(select f_name from TS_S_T_P_TrainDepSys_Type where f_id=TS_TR_B_Loco_Fittings_Base_List.f_typeid) as f_ftname from TS_TR_B_Loco_Fittings_Base_List where f_typeid=" + tl_SysDep.FocusedNode["f_id"].ToString().Substring(1) + " and f_del=0 ");
            bt_Edit.Enabled = false;
            bt_Del.Enabled = false;
        }
    }
}